<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/18
 * Time: 8:59
 */

namespace app\home\controller;

use \app\home\model\Project as projectModel;
use app\home\model\Versions;
use app\home\model\VersionsBlock;
use think\Db;
use app\common\controller\Backend;

/**
 * @title 项目中心
 * @description 接口说明
 * @group 后台管理
 */
class Project extends Backend
{
    protected $hsModel = 'Project';
    public $statusCode = [
        0  => '筹备中',
        1  => '待开发',
        2  => '开发中',
        3  => '待测试',
        4  => '测试中',
        5  => '待上线',
        6  => '已上线'
    ];
    /**
     * @title 项目入口
     * @description 接口说明
     * @author jyh
     * @url /project/getProject
     * @method POST
     * @return id:项目编号
     * @return name:项目名称
     */
    public function getProject()
    {
        $data = $this->model()->where('deleted',null)->where('status',0)->field('id,name')->order('ctime','desc')->select();
        return json(['code' => 0, 'msg' => '成功','data' => $data]);
    }

    /**
     * @title 项目中心首页
     * @description 接口说明
     * @author jyh
     * @url /project/projectCenter/:status
     * @param name:status type:int require:1 default:0 other: desc:0=项目 5=产品
     * @method POST
     * @return id:项目编号
     * @return name:项目名称
     * @return creator:创建人
     * @return ctime:创建时间
     * @return bugSum:bug总数
     * @return notBug:未关闭bug
     * @return needSum:需求总数
     * @return dealNeed:待开发需求
     * @return exploitNeed:开发中需求
     * @return testNeed:待测试需求
     * @return versions_sum:版本总数
     */
    public function projectCenter($status)
    {
        if ( $this->request->isPost() ) {
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            // 0代表项目   5代表产品
            if ( $status != 0 && $status != 5 ) {
                return json(['code' => 1, 'msg' => '参数有误']);
            }
            $group = $status == 0 ? 'project_id':'product_id';
            // 获取项目数据
            $data = Db::name('project')
                ->where('status',$status)
                ->where('deleted',null)
                ->order('ctime','desc')
                ->paginate([
                    'page' => $page,
                    'list_rows' => $limit,
                ], false)
                ->toArray();
            // 获取bug 需求等数据
            list($bugSum, $notBug, $needSum, $dealNeed, $exploitNeed, $testNeed,$versions_sum,$new_versions) = $this->statistics($group);
            $user = Db::name('user')->where('deleted',0)->column('name','id');
            // 处理数据
            foreach ($data['data'] as $key => &$value) {
                $value['creator'] = $user[$value['creator']];
                $value['ctime'] = $value['ctime'] ? date('Y-m-d H:i:s',$value['ctime']) : '';
                $value['bugSum'] = isset($bugSum[$value['id']]) ? $bugSum[$value['id']] : 0;
                $value['notBug'] = isset($notBug[$value['id']]) ? $notBug[$value['id']] : 0;
                $value['needSum'] = isset($needSum[$value['id']]) ? $needSum[$value['id']] : 0;
                $value['dealNeed'] = isset($dealNeed[$value['id']]) ? $dealNeed[$value['id']] : 0;
                $value['exploitNeed'] = isset($exploitNeed[$value['id']]) ? $exploitNeed[$value['id']] : 0;
                $value['testNeed'] = isset($testNeed[$value['id']]) ? $testNeed[$value['id']] : 0;
                $value['versions_sum'] = isset($versions_sum[$value['id']]) ? $versions_sum[$value['id']] : 0;
                $value['new_versions'] = isset($new_versions[$value['id']]) ? $new_versions[$value['id']] : '无';
            }
            return json(['code' => 0, 'msg' => '成功','data' => $data]);
        }
        return json(['code' => 0, 'msg' => '请求有误，请重新确认请求！']);
    }

    /**
     * @title 进入产品
     * @description 接口说明
     * @author jyh
     * @url /project/productInfo
     * @param name:id type:int require:1 default: other: desc:产品id
     * @method POST
     * @return id:项目编号
     * @return name:项目名称
     * @return creator:创建人
     * @return ctime:创建时间
     * @return bugSum:bug总数
     * @return notBug:未关闭bug
     * @return needSum:需求总数
     * @return dealNeed:待开发需求
     * @return exploitNeed:开发中需求
     * @return testNeed:待测试需求
     * @return versions_sum:版本总数
     */
    public function productInfo()
    {
        if ( $this->request->isPost() ) {
            $product_id = $this->request->post('id');
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            // 获取当前产品数据
            $data = \app\home\model\Project::where(['status' => 5, 'id' => $product_id])->find();
            $model = new Versions();
            // 获取当前产品的所有版本
            $versions = $model
                ->where('product_id',$product_id)
                ->order('ctime','asc')
                ->paginate([
                    'page' => $page,
                    'list_rows' => $limit,
                ], false);
            // 获取当前产品和版本的bug 需求数据
            list($bugSum, $notBug, $needSum, $dealNeed, $exploitNeed, $testNeed,$versions_sum) = $this->statistics('versions_id');
            // 获取 模块总数
            $block_sum = Db::name('VersionsBlock')->where('deleted',null)->group('versions_id')->column('count(versions_id)','versions_id');
            // 处理数据
            foreach ($versions as $key => &$value) {
                //$versions[$key]['id'] = $data['id'];
                $value['versions'] = $value['name'];
                $value['block_sum'] = isset($block_sum[$value['id']]) ? $block_sum[$value['id']] : 0;
                $value['bugSum'] = isset($bugSum[$value['id']]) ? $bugSum[$value['id']] : 0;
                $value['notBug'] = isset($notBug[$value['id']]) ? $notBug[$value['id']] : 0;
                $value['needSum'] = isset($needSum[$value['id']]) ? $needSum[$value['id']] : 0;
                $value['dealNeed'] = isset($dealNeed[$value['id']]) ? $dealNeed[$value['id']] : 0;
                $value['exploitNeed'] = isset($exploitNeed[$value['id']]) ? $exploitNeed[$value['id']] : 0;
                $value['testNeed'] = isset($testNeed[$value['id']]) ? $testNeed[$value['id']] : 0;
                $value['versions_sum'] = isset($versions_sum[$value['id']]) ? $versions_sum[$value['id']] : 0;
                $value['name'] = $data['name'];
            }
            return json(['code' => 0, 'msg' => '成功','data' => $versions]);
        }
        return json(['code' => 0, 'msg' => '请求有误，请重新确认请求！']);
    }

    /**
     * @title 项目需求/BUG 页
     * @description 接口说明
     * @author jyh
     * @url /project/getContent/:action
     * @method POST
     * @param name:id type:int require:1 default:1 other: desc:项目id
     * @param name::action type:string require:1 default:need other: desc:项目id
     * @return id:需求编号
     * @return project_id:项目id
     * @return status:需求状态 0=筹备中 1=待开发 2=开发中 3=待测试 4=测试中 5=待上线 6=已上线
     * @return product_id:关联产品
     * @return type:需求类型
     * @return describe:需求描述
     * @return source:需求来源
     * @return priority:优先级
     * @return front_user:前端开发
     * @return after_user:后端开发
     * @return creator:创建人
     * @return assigner:指派给某人
     * @return ctime:创建时间
     * @return etime:截止时间
     */
    public function getContent($action = 'need')
    {
        if ( $this->request->isPost() ) {
            // 判断是需求/bug 来定义模型
            $this->hsModel = $action == 'need' ? 'projectNeed' : 'projectBug';
            $param = $this->request->param();
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            $map = [];
            $projectName = '';
            if (isset($param['project_id'])) {
                $map['project_id'] = $param['project_id'];
            } else {
                $map['issue_id'] = isset($param['issue_id']) ? $param['issue_id'] : Db::name('IssueVersions')->order('ctime','desc')->value('id');
                $param['project_id'] = '';
            }

            if (isset($param['id'])) $map['id'] = $param['id'];

            // 取出 需求/bug 数据
            $data = Db::name($this->hsModel)
                ->where($map)
                ->where('deleted',null);
            if ($action == 'need') $data = $data->where('task_allot=0 or task_allot=4');
            $data = $data->order('ctime', 'desc')
                ->paginate([
                    'page' => $page,
                    'list_rows' => $limit,
                ], false)
                ->toArray();
            $projectName = Db::name('project')->column('name','id');
            $block = Db::name('block')->where('deleted',null)->column('name','id');
            $deploy = Db::name('deploy')->where('deleted',null)->column('name','id');
            $user = Db::name('user')->where('deleted',0)->column('name','id');
            // 判断前后端  0=前端 1=后端
            if (USER_DEPT == 0) {
                $prefix = 'front_';
            } else if (USER_DEPT == 1) {
                $prefix = 'after_';
            }
            // 判断是需求
            if ($action == 'need') {
                foreach ($data['data'] as $key => &$value) {
                    $value['status'] = $this->statusCode[$value['status']];
                    $value['project_id'] = $projectName[$value['project_id']];
                    $value['product_id'] = $projectName[$value['product_id']];
                    $value['block_id'] = $block[$value['block_id']];
                    $value['type'] = $deploy[$value['type']];
                    $value['priority'] = $deploy[$value['priority']];
                    $value['creator'] = $user[$value['creator']];
                    $value['front_user'] = $value['front_user'] ? $user[$value['front_user']] : '';
                    $value['after_user'] = $value['after_user'] ? $user[$value['after_user']] : '';
                    $value['ptime'] = $value['ptime'] ? date('Y-m-d',$value['ptime']) : '';
                    $value['etime'] = $value['etime'] ? date('Y-m-d',$value['etime']) : '';
                }
                if (isset($prefix)) {
                    // 把用户按钮权限循环加进数组
                    foreach ($data['data'] as $key => &$value) {
                        $value['isshow'] = $value[$prefix . 'predict'] > 0 ? false : true;
                        $value['isself'] = $value[$prefix . 'user'] == USER_NAME ? true : false;
                        $value['isfinish'] = $value[$prefix . 'plan'] == 100 ? false : true;
                    }
                }
            } else {
                // 把用户按钮权限循环加进数组
                foreach ($data['data'] as $key => &$value) {
                    $value['isauth'] = !in_array(USER_DEPT,[0,1]) ? true : false;
                    $value['isself'] = $value['assigner'] == USER_NAME ? true : false;
                }
            }

            if (empty($data['data'])) {
                return json(['code' => 0, 'msg' => '暂无数据','data' => $data, 'name' => $param['project_id'] ? $projectName[$param['project_id']] : '']);
            }
            return json(['code' => 0, 'msg' => '成功', 'data' => $data, 'name' => $param['project_id'] ? $projectName[$param['project_id']] : '', 'dept' => USER_DEPT]);
        }
        return json(['code' => 0, 'msg' => '请求有误，请重新确认请求！']);
    }

    /**
     * @title 获取需求/BUG 信息
     * @description 接口说明
     * @author jyh
     * @url /project/getAddInfo/:action
     * @method POST
     */
    public function getAddInfo($action = 'need')
    {
        if ( $this->request->isPost() ) {
            // 获取选项配置数据
            $deploy = new Deploy();
            $temp = $deploy->getLst();

            $needStatus['name'] = '需求状态';
            $needStatus['child'] = \app\home\model\ProjectNeed::$statusCode;
            $temp[] = $needStatus;

            $bugStatus['name'] = 'bug状态';
            $bugStatus['child'] = \app\home\model\ProjectBug::$statusCode;
            $temp[] = $bugStatus;

            $param = $this->request->post();
            // 没有项目id则返回全部项目
            if ( !isset($param['project_id']) ){
                $project = projectModel::where('status', 0)->where('deleted',null)->field('id,name')->order('ctime','desc')->select();
                return json(['code' => 0, 'msg' => '所有项目', 'product' => $project,'temp' => $temp]);
            }
            // 没有产品id则返回全部产品
            if ( !isset($param['product_id']) ){
                $product = projectModel::where('status', 5);
                $product = $product->field('id,name')->where('deleted',null)->order('ctime','desc')->select();
                return json(['code' => 0, 'msg' => '所有产品', 'product' => $product,'temp' => $temp]);
            }
            // 没有版本id则返回全部版本
            if ( !isset($param['versions_id']) ){
                $versionsModel = new Versions();
                if ( $action == 'bug' ) {
                    $versions = $versionsModel->where('product_id', $param['product_id'])->where('deleted',null)->field('id,name')->select();
                } else {
                    $versions_time = $versionsModel->where(['status' => 1, 'product_id' => $param['product_id'], 'deleted' => ['null','']])->value('ctime');
                    $versions = $versionsModel->where('ctime','>=', $versions_time)->where('product_id', $param['product_id'])->where('deleted',null)->field('id,name')->select();
                }
                return json(['code' => 0, 'msg' => '当前及最新版本', 'product' => $versions]);
            }
            // 没有模块id则返回全部模块
            if ( !isset($param['block_id']) ){
                $versionsModel = new Versions();
                $block = $versionsModel->with('block')->where('id',$param['versions_id'])->where('deleted',null)->field('id,name')->select();
                return json(['code' => 0, 'msg' => '当前版本模块', 'product' => $block]);
            }
            // 上面id都有则返回所有数据
            $product = projectModel::where('status', 5)->where('deleted',null)->field('id,name')->order('ctime','desc')->select();
            $versionsModel = new Versions();
            $versions = $versionsModel->where('product_id', $param['product_id'])->where('deleted', 'null')->field('id,name')->select();
            $block = $versionsModel->with('block')->where('deleted',null)->where('id',$param['versions_id'])->field('id,name')->select();
            $param['deleted'] = ['null',''];
            $needName = Db::name('projectNeed')->where($param)->field('id,name')->select();

            $data = ['product' => $product, 'versions' => $versions, 'block' => $block, 'needName' => $needName];
            return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
        }
        return json(['code' => 0, 'msg' => '请求有误，请重新确认请求！']);
    }

    /**
     * @title 项目bug页
     * @description 接口说明
     * @author jyh
     * @url /home/project/bugCenter
     * @method POST
     * @param name:id type:int require:1 default:1 other: desc:项目id
     * @return id:bug编号
     * @return project_id:项目id
     * @return status:bug状态 0=未确认 1=已确认 2=已解决 3=已关闭 4=激活
     * @return product_id:所属产品
     * @return type:bug类型
     * @return degree:严重程度
     * @return priority:优先级
     * @return title:bug标题
     * @return creator:创建人
     * @return assigner:指派给某人
     * @return ctime:创建时间
     * @return etime:截止时间
     */

    /**
     * @title 项目中心增加
     * @description 接口说明
     * @author jyh
     * @url /project/add
     * @method POST
     */
    public function add()
    {
        return parent::add(); // TODO: Change the autogenerated stub
    }

    /**
     * @title 项目中心删除
     * @description 接口说明
     * @author jyh
     * @url /project/del/:id
     * @method POST
     * @param name:id type:int require:1 default: other: desc:项目id
     */
    public function del($ids = "")
    {
        // 模型事件 在插入前执行图片插入
        $this->model()::event('after_delete', function ($result) {
            $id = $result->id;
            $status = $result->status;
            $field = $status == 0 ? 'project_id' : 'product_id';
            $needModel = new \app\home\model\ProjectNeed();
            $bugModel = new \app\home\model\ProjectBug();
            $needModel->where($field,$id)->update(['deleted' => time()]);
            $bugModel->where($field,$id)->update(['deleted' => time()]);
        });
        return parent::del($ids); // TODO: Change the autogenerated stub
    }

    /**
     * @title 项目中心修改
     * @description 接口说明
     * @author jyh
     * @url /project/edit/:id
     * @method POST
     * @param name:id type:int require:1 default: other: desc:项目id
     * @param name:name type:string require:1 default: other: desc:项目名称
     */
    public function edit($ids = "")
    {
        return parent::edit($ids); // TODO: Change the autogenerated stub
    }

}